﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ControlLibrary;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }

            //eucl1.EmbeddedUserControl.LoadData(this.TextBox1.Text);
        }

        private void Load()
        {
            string[] dataSource = new string[] {"0", "2", "1"};
            Repeater1.DataSource = dataSource;
            Repeater1.DataBind();

            foreach (string item in dataSource)
            {
                EmbeddedUserControlLoader load = null;

                load = new EmbeddedUserControlLoader(item, "DynamicControlLibrary", "DynamicControlLibrary",
                                                                    "HelloControl");

                //load = new DynamicControlLibrary.EmbeddedUserControlLoader("FeedbackControl", "FeedbackControl",
                //                                                    "FeedbackControl");
                load.ControlLoaded += new EventHandler(load_ControlLoaded);

                PlaceHolder.Controls.Add(load);
            }
        }

        private void load_ControlLoaded(object sender, EventArgs e)
        {
            EmbeddedUserControlLoader load = (sender as EmbeddedUserControlLoader);
            load["Message"] = "asdfasdf";
            Response.Write(load["Message"]);
        }

        protected override void OnInit(EventArgs e)
        {
            Load();
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            //Load();
            base.OnLoad(e);
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            EmbeddedUserControlPlaceHolder ph = e.Item.FindControl("euclPlaceHolder") as EmbeddedUserControlPlaceHolder;
            if (ph != null)
            {
                EmbeddedUserControlLoader load = null;

                //load = new DynamicControlLibrary.EmbeddedUserControlLoader("DynamicControlLibrary", "DynamicControlLibrary",
                //                                                    string.Format("HelloControl{0}",
                //                                                                  e.Item.ItemIndex));
                load = new EmbeddedUserControlLoader("DynamicControlLibrary", "DynamicControlLibrary", "HelloControl");

                ph.Controls.Add(load);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (Control control in PlaceHolder.Controls)
            {
                if (control is EmbeddedUserControlLoader)
                {
                    (control as EmbeddedUserControlLoader).EmbeddedUserControl.Save();
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in Repeater1.Items)
            {
                EmbeddedUserControlPlaceHolder ph =
                    item.FindControl("euclPlaceHolder") as EmbeddedUserControlPlaceHolder;

                ph.Loader.EmbeddedUserControl.Save();
            }
        }
    }
}